Skip to content

feat(hooks): add ModelStreamChunkEvent for streaming observability#2159

Open
en-yao wants to merge 1 commit intostrands-agents:mainfrom
en-yao:feat/hooks-model-stream-chunk-event
Open

feat(hooks): add ModelStreamChunkEvent for streaming observability#2159
en-yao wants to merge 1 commit intostrands-agents:mainfrom
en-yao:feat/hooks-model-stream-chunk-event

Conversation

@en-yao
Copy link
Copy Markdown
Contributor

@en-yao en-yao commented Apr 18, 2026

Description

Hook providers need to observe individual streaming chunks during model response generation for use cases like real-time logging, token metrics, and content filtering. Currently, BeforeModelCallEvent and AfterModelCallEvent bookend the streaming call, but nothing fires while the stream is active.

ModelStreamChunkEvent is now available for hook providers to observe each raw streaming chunk:

from strands.hooks import HookProvider, HookRegistry
from strands.hooks.events import ModelStreamChunkEvent

class StreamingLogger(HookProvider):
    def register_hooks(self, registry: HookRegistry) -> None:
        registry.add_callback(ModelStreamChunkEvent, self.on_chunk)

    def on_chunk(self, event: ModelStreamChunkEvent) -> None:
        if "contentBlockDelta" in event.chunk:
            print(f"Received delta: {event.chunk}")

agent = Agent(hooks=[StreamingLogger()])

The event fires for all streaming chunks (including metadata, message start/stop) after yielding the internal typed event, preserving existing consumer behavior. This achieves parity with the TypeScript SDK's ModelStreamUpdateEvent.

Related Issues

#1819

Documentation PR

N/A

Type of Change

New feature

Testing

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@en-yao en-yao force-pushed the feat/hooks-model-stream-chunk-event branch from 2ba926e to 8086f6f Compare April 18, 2026 13:14
@github-actions github-actions Bot added size/m and removed size/m labels Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant